<template>
  <el-scrollbar style="height:100%;">
    <div class="container">
      <div class="markdown-body">
        <!-- <vue-markdown :source="msg"></vue-markdown> -->
        <div class="goBack">
          <i class="iconfont icon-zhuye" @click="goBackHome()"></i>
        </div>
        <div class="title">发布文章</div>
        <div class="ArticleArea">
          <el-form :model="ruleForm" :rules="rules" ref="nameAndEmail" class="s-content">
            <el-form-item label="文章标题" prop="articleTitle" class="form-item-name">
              <el-input v-model="ruleForm.articleTitle" placeholder="请输入文章标题"></el-input>
            </el-form-item>
            <el-form-item label="文章类别" prop="articleClass" class="form-item-email">
              <!-- <el-input v-model="ruleForm.articleClass" placeholder="请输入文章类别"></el-input> -->
              <el-select v-model="ruleForm.articleClass" placeholder="请选择文章类别" class="cate_select">
                <el-option label="学习记录" value="学习记录"></el-option>
                <el-option label="生活琐事" value="生活琐事"></el-option>
                <el-option label="有感而发" value="有感而发"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-form :model="ruleForm" :rules="rules" ref="comment" class="demo-ruleForm">
            <el-form-item label="文章内容" prop="desc">
              <!-- <el-input type="textarea" v-model="ruleForm.desc" placeholder="说点什么吧"></el-input> -->
              <mavon-editor v-model="ruleForm.desc" @imgAdd="$imgAdd" ref=md />
            </el-form-item>
          </el-form>
          <!-- 上传图片 -->
          <el-upload class="upload-demo" action="http://localhost:3000/api/upload" multiple :limit="3"
            :file-list="fileList" :on-success="getFileName" ref="upload-fm">
            <el-button slot="trigger" size="small" type="primary">点击上传博客封面</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
          <el-upload class="upload-demo" action="http://localhost:3000/api/upload" multiple :limit="3"
            :file-list="fileList" :on-success="getFileNameCate" ref="upload-cate">
            <el-button slot="trigger" size="small" type="primary">点击上传分类封面</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
          <div class="submitBtn">
            <el-button type="primary" @click="refreshComment('nameAndEmail','comment')">重置</el-button>
            <el-button type="primary" @click="submitComment()">提交</el-button>
          </div>
        </div>
      </div>
    </div>
  </el-scrollbar>
</template>

<script>
// import 'highlight.js/styles/github.css'
// import 'github-markdown-css'
// import VueMarkdown from 'vue-markdown'
export default {
  data() {
    return {
      // msg: '# 标题一级\n## 标题二级\n### 标题三级\n#### 标题四级\n##### 标题五级\n###### 标题六级'
      // 上传的图片列表
      fileList: [],
      // 上传的图片的文件名
      uploadImg: '',
      // 上传的分类封面
      uploadCate: '',
      // form表单数据
      ruleForm: {
        desc: '',
        articleTitle: '',
        articleClass: ''
      },
      // form表单验证规则
      rules: {
        desc: [
          { required: true, message: '请输入想要发表的内容', trigger: 'blur' }
        ],
        articleTitle: [
          { required: true, message: '请输入想要发表的内容', trigger: 'blur' }
        ],
        articleClass: [
          { required: true, message: '请输入想要发表的内容', trigger: 'blur' }
        ]
      },
      // markdown编辑器
      // value: '',
      defaultData: 'preview'
    }
  },
  methods: {
    // 发布文章
    submitComment() {
      if (this.ruleForm.name === '' || this.ruleForm.email === '' || this.ruleForm.desc === '') {
        this.$message.error('请填写相关的信息！')
      } else {
        const date = new Date()
        const year = date.getFullYear()
        const month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
        const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
        const min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
        const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        this.$http.post('articleRelease', {
          title: this.ruleForm.articleTitle,
          category: this.ruleForm.articleClass,
          add_time: `${year}年${month}月${day}日${hour}:${min}:${second}`,
          visits: 0,
          content: this.ruleForm.desc,
          img: 'http://localhost:3000/upload/' + this.uploadImg,
          intro_content: this.ruleForm.desc,
          author: 'Kpods',
          dianzan: 0,
          add_time_yd: `${year}年${month}月${day}日`,
          img_cates: 'http://localhost:3000/upload/' + this.uploadCate
        }).then(response => {
          if (response.status === 200) {
            this.$message({
              type: 'success',
              message: '文章发表成功!'
            })
            this.refreshComment('nameAndEmail', 'comment')
            this.clearFiles()
          }
        })
      }
    },
    // 重置表单
    refreshComment(formName1, formName2) {
      this.$refs[formName1].resetFields()
      this.$refs[formName2].resetFields()
    },
    // 返回主页
    goBackHome() {
      this.$router.push('/BsHome')
    },
    // 获取上传的图片链接
    getFileName(response, file, fileList) {
      var name = response.url
      var nameArr = name.split('\\')
      this.uploadImg = nameArr[nameArr.length - 1]
    },
    getFileNameCate(response, file, fileList) {
      var name = response.url
      var nameArr = name.split('\\')
      this.uploadCate = nameArr[nameArr.length - 1]
    },
    // 清空上传的图片列表
    clearFiles() {
      this.$refs['upload-fm'].clearFiles()
      this.$refs['upload-cate'].clearFiles()
    },
    // mavon-editor 上传图片
    $imgAdd(pos, file) {
      var formdata = new FormData()
      formdata.append('file', file)
      this.$http.post('/upload', formdata).then((res) => {
        var img = res.data.url
        var imgUrl = img.split('\\')
        const url = 'http://localhost:3000/upload/' + imgUrl[imgUrl.length - 1]
        this.$refs.md.$img2Url(pos, url)
      })
    }
  }
  // components: {
  //   VueMarkdown
  // }
}
</script>

<style lang="less" scoped>
/deep/.el-scrollbar__wrap {
  overflow-x: hidden;
}
.markdown-body {
  // height: 100%;
  padding: 50px 150px;
  box-sizing: border-box;
  background-image: linear-gradient(
    to right,
    rgba(94, 249, 254, 0.6),
    rgba(253, 255, 106, 0.6),
    rgba(255, 106, 106, 0.6)
  );
  .goBack {
    float: left;
    i {
      font-size: 30px;
      color: #1296db;
      cursor: pointer;
    }
  }
  .title {
    font-size: 26px;
    text-align: center;
    color: #333;
    margin-bottom: 20px;
  }
}
/deep/.v-note-wrapper {
  box-shadow: none !important;
  background-image: none;
  padding: 0;
  display: block;
  margin-top: 50px;
}
/deep/.v-note-panel {
  min-height: 450px;
}
/deep/.content-input-wrapper {
  height: 100%;
}
.ArticleArea {
  /deep/ textarea {
    height: 500px;
  }
  /deep/ .el-form-item__label {
    padding: 0px;
  }
  /deep/ .el-input__inner {
    margin-left: 20px;
    max-width: 800px;
  }
  .submitBtn {
    text-align: center;
    margin-top: 50px;
    .el-button {
      &:nth-child(1) {
        margin-right: 50px;
      }
    }
  }
  .upload-demo {
    margin-bottom: 20px;
  }
  /deep/.el-input__inner {
    max-width: none;
    width: 1180px;
  }
}
</style>
